package com.fg.cloud.factory.controller.excel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.poi.ExcelRead;
import com.fg.cloud.common.poi.ExcelUtil;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.AgencyEntity;
import com.fg.cloud.manager.service.agency.AgencyAreaService;
import com.fg.cloud.manager.service.agency.AgencyLevelService;
import com.fg.cloud.manager.service.agency.AgencyService;

/**
 * Description:导入经销商
 * @author dingjiang
 * @date 2017年9月15日下午5:55:54
 */
@Controller
@RequestMapping("/agencyImport")
public class AgencyImportController extends BaseController{
	
	@Autowired
	private AgencyService agencyService;
	@Autowired
	private AgencyLevelService agencylevelService;
	@Autowired
	private AgencyAreaService agencyAreaService;
	
	
	@RequestMapping(value = "/importAgency", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void importAgency( @Param("file")MultipartFile file, Integer row, HttpServletRequest req, 
			HttpServletResponse resp) throws IOException{
		//验证非空
		if( file == null ){ 
			doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
			return ;
		}
		String name = file.getOriginalFilename();  
	    long size = file.getSize();
	    Long facId = getCurrentFactoryUser().getFacId();
	    //验证path和内容不为空
	    if( name == null || ExcelUtil.EMPTY.equals(name) && size == 0 ){  
	    	doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
			return ;
	    }
	    // 读取Excel数据到List中  
	    List<ArrayList<Object>> list = new ExcelRead().readExcel(file, row, null, 0, null);
	    // list中存的就是excel中的数据，可以根据excel中每一列的值转换成你所需要的值（从0开始）
		for( int i=0; i<list.size(); i++ ){
	    	ArrayList<Object> arrayList = list.get(i);
	    	AgencyEntity entity = new AgencyEntity();
	    	
    		entity.setFacId(facId);
    		entity.setAgeNumber((String)arrayList.get(0));
    		entity.setAgeName((String)arrayList.get(1));
    		if ( arrayList.get(2) != null ) {
				entity.setPlaceorder((Integer)arrayList.get(2));
			}
    		if ( arrayList.get(3) != null ) {
				entity.setStatus((Integer)arrayList.get(3));
			}
    		Long levelId = agencylevelService.selectId((String)arrayList.get(4), facId);
    		entity.setLevelId(levelId);
    		Long areaId = agencyAreaService.selectId((String)arrayList.get(5), facId);
    		entity.setAreaId(areaId);
    		entity.setProvince((String)arrayList.get(6));
    		entity.setCity((String)arrayList.get(7));
    		entity.setArea((String)arrayList.get(8));
    		entity.setAddress((String)arrayList.get(9));
    		if ( arrayList.get(10) != null ) {
				entity.setAddressStatus((Integer)arrayList.get(10));
			}
    		entity.setContact((String)arrayList.get(11));
    		if ( arrayList.get(12) != null ) {
				entity.setTel((String)arrayList.get(12));
			}
    		if ( arrayList.get(13) != null ) {
				entity.setPhone((String)arrayList.get(13));
			}
    		entity.setZipcode((String)arrayList.get(14));
    		if ( arrayList.get(15) != null ) {
				entity.setBillTitle((String)arrayList.get(15));
			}
    		if ( arrayList.get(16) != null ) {
				entity.setBankName((String)arrayList.get(16));
			}
    		if ( arrayList.get(17) != null ) {
				entity.setBankNo((String)arrayList.get(17));
			}
    		if ( arrayList.get(18) != null ) {
				entity.setTaxNumber((String)arrayList.get(18));
			}
    		if ( arrayList.get(19) != null ) {
    			entity.setTaxType((Integer)arrayList.get(19));
			}
    		if ( arrayList.get(20) != null ) {
				entity.setTaxAddress((String)arrayList.get(20));
			}
    		entity.setCreator(getCurrentFactoryUser().getId());
    		entity.setUpdater(getCurrentFactoryUser().getId());
    		
    		boolean flag = agencyService.importAgency(entity);
    		doResult(resp, flag, flag?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
    				flag?ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
	    }
	}
	

	
	/*TdProjectVo vo = null;
    StudentVo stuVo = new StudentVo();
    StaffSearch staffSearch = new StaffSearch();
    for(ArrayList<String> arr:list){                        
           vo= new TdProjectVo();
           Long id = null;
           
           try{
           	id = Long.valueOf(arr.get(0));
           }catch(Exception e){
           	result.setMessage("表中数据有错误,提交失败!");
           	return result;
           }
           if(id == null){
           	result.setMessage("表中数据有错误,提交失败!");
           	return result;
           }
           stuVo.setId(id);//每一行的第一个单元格
           StudentVo stu = studentService.getVo_AdminEdit(id);
           if(stu == null){
           	result.setMessage("表中数据有错误,提交失败!");
           	return result;
           }
           vo.setId(stu.getProjectTestId());
           Long project1 = null;
           try{
           	project1 = Long.valueOf(arr.get(2));
           }catch(Exception e){
           	result.setMessage("表中数据有错误,提交失败!");
           	return result;
           }
           vo.setProject1(project1);
           staffSearch.setEqStaffName(arr.get(3));
           StaffVo staffVo = staffService.queryVo_Auto(staffSearch);
           if(staffVo == null){
           	result.setMessage("表中数据有错误,提交失败!");
           	return result;
           }
           vo.setStaff1(staffVo.getId());
           tdProjectService.saveBatchInsert1(vo); 
       } 
    result.setMessage("");  
	 return result;*/
}
